/****************************************************************************
 *
 *   Copyright (c) 2025 H743 Flight Control Project. All rights reserved.
 *   Adapted from PX4 Autopilot Project
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in
 *    the documentation and/or other materials provided with the
 *    distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 *
 ****************************************************************************/

/**
 * @file px4_sem.c
 * 
 * PX4 Semaphore Implementation for FreeRTOS
 */

#include "px4_platform_common/px4_sem.h"

int px4_sem_init(px4_sem_t *sem, int pshared, unsigned int value)
{
    if (sem == NULL) {
        return -1;
    }

    /* Create FreeRTOS counting semaphore */
    sem->sem = xSemaphoreCreateCounting(0xFFFFFFFF, value);
    
    if (sem->sem == NULL) {
        return -1;
    }

    UNUSED(pshared);  /* Not used in FreeRTOS */
    return 0;
}

int px4_sem_destroy(px4_sem_t *sem)
{
    if (sem == NULL || sem->sem == NULL) {
        return -1;
    }

    vSemaphoreDelete(sem->sem);
    sem->sem = NULL;
    return 0;
}

int px4_sem_wait(px4_sem_t *sem)
{
    if (sem == NULL || sem->sem == NULL) {
        return -1;
    }

    /* Block indefinitely until semaphore is available */
    if (xSemaphoreTake(sem->sem, portMAX_DELAY) == pdTRUE) {
        return 0;
    }

    return -1;
}

int px4_sem_trywait(px4_sem_t *sem)
{
    if (sem == NULL || sem->sem == NULL) {
        return -1;
    }

    /* Try to take semaphore without blocking */
    if (xSemaphoreTake(sem->sem, 0) == pdTRUE) {
        return 0;
    }

    return -1;  /* Semaphore not available */
}

int px4_sem_timedwait(px4_sem_t *sem, uint32_t timeout_ms)
{
    if (sem == NULL || sem->sem == NULL) {
        return -1;
    }

    /* Convert milliseconds to ticks */
    TickType_t ticks = pdMS_TO_TICKS(timeout_ms);
    
    if (xSemaphoreTake(sem->sem, ticks) == pdTRUE) {
        return 0;
    }

    return -1;  /* Timeout or error */
}

int px4_sem_post(px4_sem_t *sem)
{
    if (sem == NULL || sem->sem == NULL) {
        return -1;
    }

    /* Give semaphore */
    if (xSemaphoreGive(sem->sem) == pdTRUE) {
        return 0;
    }

    return -1;
}

int px4_sem_getvalue(px4_sem_t *sem, int *sval)
{
    if (sem == NULL || sem->sem == NULL || sval == NULL) {
        return -1;
    }

    /* Get current semaphore count */
    *sval = (int)uxSemaphoreGetCount(sem->sem);
    return 0;
}

int px4_sem_setprotocol(px4_sem_t *sem, int protocol)
{
    if (sem == NULL || sem->sem == NULL) {
        return -1;
    }

    /* FreeRTOS counting semaphores don't support priority protocols
     * This function is provided for API compatibility only */
    UNUSED(protocol);
    return 0;  /* Always succeed for compatibility */
}

